<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>招聘发布</el-breadcrumb-item>
      <el-breadcrumb-item>招聘列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >招聘发布</el-button
          >
        </el-col>
      </el-row>

      <!-- 用户列表区 -->
      <el-table border stripe :data="recruitsendlist">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="岗位" prop="jod"></el-table-column>
        <el-table-column label="岗位要求" prop="jodRequire"></el-table-column>
        <el-table-column label="城市" prop="city"></el-table-column>
        <el-table-column label="薪资" prop="salary"></el-table-column>
        <el-table-column label="招聘负责人" prop="chargeMan"></el-table-column>
        <el-table-column label="联系电话" prop="phone"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button type="info" icon="el-icon-share" size="mini"></el-button>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加用户 -->
    <el-dialog
      @close="addDialogClosed"
      title="招聘发布"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
      >
        <el-form-item label="岗位" prop="jod">
          <el-input v-model="addForm.jod"></el-input>
        </el-form-item>
        <el-form-item label="岗位要求" prop="jodRequire">
          <el-input
            v-model="addForm.jodRequire"
            type="textarea"
            :rows="4"
            maxlength="200"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-select v-model="addForm.city" placeholder="请选择">
            <el-option-group
              v-for="group in options"
              :key="group.label"
              :label="group.label"
            >
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="薪资" prop="salary">
          <el-select v-model="addForm.salary" placeholder="请选择">
            <el-option-group
              v-for="group in salarys"
              :key="group.label"
              :label="group.label"
            >
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="招聘负责人" prop="chargeMan">
          <el-input v-model="addForm.chargeMan"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEnterpriseList">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    //验证电话号码的规则
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^1[3|4|5|7|8][0-9]{9}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的手机号码"));
    };
    return {
      //信息发布表单的验证规则
      addFormRules: {
        city: [
          { required: true, message: "请选择工作城市", trigger: "change" },
        ],
        salary: [
          { required: true, message: "请选择薪资范围", trigger: "change" },
        ],
        job: [{ required: true, message: "请输入工作岗位", trigger: "blur" }],
        jobRequire: [
          { required: true, message: "请输入岗位要求", trigger: "blur" },
        ],
        chargeMan: [
          { required: true, message: "请输入招聘负责人", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
          {
            validator: checkMobile,
            message: "手机号码不正确, 请重新输入",
            trigger: "blur",
          },
        ],
      },
      //信息发布表单
      addForm: {
        enterpriseId: "",
        enterpriseName: "",
        jod: "",
        jodRequire: "",
        city: "",
        salary: "",
        chargeMan: "",
        phone: "",
        email: "1",
      },
      //招聘信息发布列表
      recruitsendlist: [],
      //控制添加对话框的显示与隐藏
      addDialogVisible: false,
      //表单域
      textarea2: "",
      //   薪资情况
      salarys: [
        {
          label: "热门城市",
          options: [
            {
              value: "3000-5000",
              label: "3000-5000",
            },
            {
              value: "5000-7000",
              label: "5000-7000",
            },
          ],
        },
        {
          label: "城市名",
          options: [
            {
              value: "7000-9000",
              label: "7000-9000",
            },
            {
              value: "9000-12000",
              label: "9000-12000",
            },
            {
              value: "12000-15000",
              label: "12000-15000",
            },
            {
              value: "15000-30000",
              label: "15000-30000",
            },
          ],
        },
      ],
      salary: "",
      //城市选择
      options: [
        {
          label: "热门城市",
          options: [
            {
              value: "上海",
              label: "上海",
            },
            {
              value: "北京",
              label: "北京",
            },
          ],
        },
        {
          label: "城市名",
          options: [
            {
              value: "成都",
              label: "成都",
            },
            {
              value: "深圳",
              label: "深圳",
            },
            {
              value: "广州",
              label: "广州",
            },
            {
              value: "大连",
              label: "大连",
            },
          ],
        },
      ],
      city: "",
    };
  },
  created() {
    if (sessionStorage.getItem("usermeg")) {
      let usermeg = JSON.parse(sessionStorage.getItem("usermeg"));
      this.$store.commit("setUserdata", usermeg);
      console.log("2", usermeg);
    }
    this.recruitSendList();
  },
  methods: {
    async recruitSendList() {
      let id = this.$store.state.userdata.id;
      const { data: res } = await this.$http.get(
        `Enterprise/RecruitmentList/${id}`
      );
      if (res.code !== 2002) {
        return this.$message.error("获取招聘发布数据列表失败！");
      } else {
        this.recruitsendlist = res.objData;
      }
    },
    addEnterpriseList() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;

        this.addForm.enterpriseId = this.$store.state.userdata.id;
        this.addForm.enterpriseName = this.$store.state.userdata.enterprise.enterpriseName;
        console.log(this.addForm);
        const { data: res } = await this.$http.post(
          `/Enterprise/recruInfo`,
          this.addForm
        );
        if (res.code !== 2002) {
          this.$message.error("添加招聘信息失败！");
        } else {
          this.$message.success("添加招聘信息成功！");
          this.addDialogVisible = false;
          this.recruitSendList();
        }
      });
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 20px;
}
.el-table {
  margin-top: 10px;
}
.el-select {
  position: relative;
  left: -200px;
}
</style>